package com.jwdf.uda.push;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.jwdf.uda.R;
import com.jwdf.uda.ui.activity.MainActivity;
import com.jwdf.uda.utils.StringUtils;
import com.umeng.common.message.Log;
import com.umeng.message.UmengBaseIntentService;
import com.umeng.message.entity.UMessage;

import org.android.agoo.client.BaseConstants;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by Administrator on 2016/6/29.
 */
public class PushIntentService extends UmengBaseIntentService {
    private static final String TAG = "Push";

    @Override
    protected void onMessage(Context context, Intent intent) {
        // 需要调用父类的函数，否则无法统计到消息送达
        super.onMessage(context, intent);
        try {
            //可以通过MESSAGE_BODY取得消息体
            String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY);
            Log.e(TAG, "onMessage:" + message);
            UMessage msg = new UMessage(new JSONObject(message));
            //发送通知信息
            createMessageNotify(context, msg, message);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    private void createMessageNotify(Context context, UMessage msg, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setAutoCancel(true);
        builder.setSmallIcon(R.mipmap.icon_app);
        builder.setContentTitle(msg.title);
        builder.setContentText(msg.text);
        builder.setTicker(msg.ticker);
        builder.setOngoing(true);

        //获取intent
        Intent intent = createIntent(context, msg);
        //add MESSAGE
        intent.putExtra("uMessage", message);

        int mid = msg.msg_id.hashCode();
        PendingIntent pi = PendingIntent.getActivity(context, mid, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pi);

        if (msg.play_sound && msg.play_vibrate && msg.play_lights) {
            builder.setDefaults(Notification.DEFAULT_ALL);
        } else if (!msg.play_vibrate && !msg.play_lights) {
            builder.setDefaults(Notification.DEFAULT_SOUND);//语音
        } else if (!msg.play_sound && !msg.play_lights) {
            builder.setDefaults(Notification.DEFAULT_VIBRATE);//震动
        } else if (!msg.play_sound && !msg.play_sound) {
            builder.setDefaults(Notification.DEFAULT_LIGHTS);//呼吸灯
        } else {
            if (msg.play_sound) {
                builder.setDefaults(Notification.DEFAULT_SOUND);//语音
            }

            if (msg.play_vibrate) {
                builder.setDefaults(Notification.DEFAULT_VIBRATE);//震动
            }

            if (msg.play_lights) {
                builder.setDefaults(Notification.DEFAULT_LIGHTS);//呼吸灯
            }
        }

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(mid, builder.build());
    }

    //处理消息
    private Intent createIntent(Context context, UMessage msg) {
        Map<String, String> map = msg.extra;
        if (map != null) {
            String afteropen = StringUtils.filterNullAndTrim(map.get("afteropen"));
            String activity = StringUtils.filterNullAndTrim(map.get("activity"));
            String extra = StringUtils.filterNullAndTrim(map.get("extra"));
            int detailId;
            String detailUrl = "";
            if (!StringUtils.isNullEmpty(afteropen)) {
                if ("go_activity".equals(afteropen)) {
                    try {
                        JSONObject jObject = new JSONObject(extra);
                        if (jObject.has("detailId")) {
                            detailId = jObject.optInt("detailId");
                            return buildGoActivityIntent(context, activity, detailId);
                        } else if (jObject.has("url")) {
                            detailUrl = jObject.optString("url");
                            return buildGoWebActivityIntent(context, activity, detailUrl);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if ("go_url".equals(afteropen)) {
                    try {
                        JSONObject jObject = new JSONObject(extra);
                        detailUrl = jObject.optString("url");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    return buildGoWebActivityIntent(context, "com.jwdf.uda.ui.activity.H5Activity", detailUrl);
                }
            }
            //TODO
        }
        return buildMainIntent(context);
    }

    private Intent buildGoActivityIntent(Context context, String activity, int detailId) {
        Intent intent = new Intent();
        intent.setClassName(context, activity);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("id", detailId);
        return intent;
    }

    private Intent buildGoWebActivityIntent(Context context, String activity, String url) {
        Intent intent = new Intent();
        intent.setClassName(context, activity);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("url", url);
        return intent;
    }

    private Intent buildMainIntent(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

}
